package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.app.domain.AppActivity;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.fdfs.ImageUtil;
import com.study.common.utils.MessageUtils;
import com.study.common.utils.StringUtils;
import com.study.app.domain.bo.AppActivityBo;
import com.study.app.domain.vo.AppActivityVo;
import com.study.app.mapper.AppActivityMapper;
import com.study.app.service.IAppActivityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * App弹窗信息Service业务层处理
 *
 * @author study
 * @date 2023-09-14
 */
@RequiredArgsConstructor
@Service
public class AppActivityServiceImpl implements IAppActivityService {

    private final AppActivityMapper baseMapper;

    private final ImageUtil imageUtil;

    /**
     * 查询App弹窗信息
     */
    @Override
    public AppActivityVo queryById(Long id) {
        AppActivityVo appActivityVo = baseMapper.selectVoById(id);
        appActivityVo.setSourceContent(imageUtil.location(appActivityVo.getSourceContent()));
        return appActivityVo;
    }

    /**
     * 查询App弹窗信息列表
     */
    @Override
    public TableDataInfo<AppActivityVo> queryPageList(AppActivityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppActivity> lqw = buildQueryWrapper(bo);
        Page<AppActivityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (AppActivityVo record : result.getRecords()) {
            record.setSourceContent(imageUtil.location(record.getSourceContent()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询App弹窗信息列表
     */
    @Override
    public List<AppActivityVo> queryList(AppActivityBo bo) {
        LambdaQueryWrapper<AppActivity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppActivity> buildQueryWrapper(AppActivityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppActivity> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getActicityTitle()), AppActivity::getActicityTitle, bo.getActicityTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getActicityDescription()), AppActivity::getActicityDescription, bo.getActicityDescription());
        lqw.eq(bo.getSourceType() != null, AppActivity::getSourceType, bo.getSourceType());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceContent()), AppActivity::getSourceContent, bo.getSourceContent());
        lqw.eq(bo.getSourceShopId() != null, AppActivity::getSourceShopId, bo.getSourceShopId());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceLink()), AppActivity::getSourceLink, bo.getSourceLink());
        lqw.eq(bo.getActionStatus() != null, AppActivity::getActionStatus, bo.getActionStatus());
        return lqw;
    }

    /**
     * 新增App弹窗信息
     */
    @Override
    public Boolean insertByBo(AppActivityBo bo) {
        AppActivity add = BeanUtil.toBean(bo, AppActivity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改App弹窗信息
     */
    @Override
    public Boolean updateByBo(AppActivityBo bo) {
        AppActivity update = BeanUtil.toBean(bo, AppActivity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppActivity entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除App弹窗信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 修改上架下架前校验判断
     */
    private void validBeforeSave() {
        //TODO 做一些数据校验,如唯一约束
        AppActivityVo activityVo = baseMapper.selectVoOne(new LambdaQueryWrapper<AppActivity>().eq(AppActivity::getActionStatus, 2));
        if (activityVo != null){
            throw new RuntimeException(MessageUtils.message("system.window.inexistence"));
        }

    }

    @Override
    public Boolean updateById(AppActivityBo bo) {
        AppActivity update = BeanUtil.toBean(bo, AppActivity.class);
        if (update.getActionStatus() == 2){
            validBeforeSave();
        }
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public AppActivityVo queryInfo() {
        AppActivityVo appActivityVo = baseMapper.selectVoOne(new LambdaQueryWrapper<AppActivity>().eq(AppActivity::getActionStatus, 2));
        appActivityVo.setSourceContent(imageUtil.location(appActivityVo.getSourceContent()));
        return appActivityVo;
    }
}
